<template>
	<view>
		<button @click="initBlueTooth" size="mini">扫描蓝牙</button>
		<button @click="stopSearchBluetooth" size="mini">停止扫描</button>
		<view class="item" v-for="item in bluetoothDeviceList" :key="item.deviceId" @click="createConnection(item)">
			<view>
				<text>id: {{ item.deviceId }}</text>
			</view>
			<view>
				<text>name: {{ item.name }}</text>
			</view>
		</view>
	</view>
</template>

<script>

	export default {
		data() {
			return {
				bluetoothDeviceList: [],
				title: '',
			}
		},
		mounted() {

		},
		onShow() {
			this.openBlueTooth()
		},
		onHide() {

		},
		onLoad(options) {
			this.title = options.title
		},
		onUnload() {

		},
		methods: {
			// 初始化蓝牙模块
			initBlueTooth() {
				let that = this
				plus.bluetooth.openBluetoothAdapter({
					success: (res) => {
						console.log('蓝牙初始化成功');
						that.startSearchBluetooth()
					},
					fail: () => {
						console.log('蓝牙初始化失败')
					}
				});
			},
			// 打开蓝牙
			openBlueTooth() {
				let that = this
				plus.bluetooth.openBluetoothAdapter({
					success: (res) => {
						console.log('蓝牙已打开');
						that.initBlueTooth()
					},
					fail: (err) => {
						console.log('请打开蓝牙');
					},
				});
			},
			// 开始搜索蓝牙设备
			startSearchBluetooth() {
				this.bluetoothDeviceList=[]
				let that = this
				uni.startBluetoothDevicesDiscovery({
					allowDuplicatesKey: false,
					interval: 0,
					success: (res) => {
						console.log('开始搜索蓝牙设备');
						setTimeout(() => {
							that.listenerDeviceFound()
						}, 1000)
					}
				});
			},
			// 停止搜索蓝牙设备
			stopSearchBluetooth() {
				plus.bluetooth.stopBluetoothDevicesDiscovery({
					success: function(e) {
						console.log('stop discovery success: ' + JSON.stringify(e));
						plus.bluetooth.closeBluetoothAdapter({
							success: function(e) {
								console.log('close success: ' + JSON.stringify(e));
							},
							fail: function(e) {
								console.log('close failed: ' + JSON.stringify(e));
							}
						});
					},
					fail: function(e) {
						console.log('stop discovery failed: ' + JSON.stringify(e));
					}
				});
			},
			// 搜索到新设备
			listenerDeviceFound() {
				let that = this
				uni.onBluetoothDeviceFound(function(e) {
					that.getBluetoothDevices()
				});
			},
			// 获取已搜索到的蓝牙设备
			getBluetoothDevices() {
				let that = this
				uni.getBluetoothDevices({
					success: function(e) {
						// var devices = e.devices;
						let arr = that.bluetoothDeviceList;
						let devices = [];
						let list = e.devices;
						for (let i = 0; i < list.length; ++i) {
							if (list[i].name && list[i].name != "未知设备") {
								let arrNew = arr.filter((item) => {
									return item.deviceId == list[i].deviceId;
								});
								if (arrNew.length == 0) {
									devices.push(list[i]);
								}
							}
						}
						that.bluetoothDeviceList = arr.concat(devices);
					},
					fail: function(e) {
						console.log('get devices failed: ' + JSON.stringify(e));
					}
				});
			},
			createConnection(item) {
				let that=this
				uni.setStorage({
					key: that.title,
					data: item,
					success: () => {
						uni.redirectTo({
							url: '/pages/device/index'
						})
						that.stopSearchBluetooth()
					},
					fail: () => {
						uni.showToast('绑定失败，请稍后重试')
					}
				})
			}
		}
	}
</script>

<style>
	.item {
		box-sizing: border-box;
		padding: 10rpx;
		font-size: 28rpx;
		border-bottom: 1px solid #ccc;
		flex: 1
	}
</style>